package kr.hkit.powerjava.chapter25.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class QuizServer {
	public static void main(String[] args) throws IOException {
		ServerSocket serverSoket =null;
		Socket clientSoket= null;
		PrintWriter out = null;
		BufferedReader in = null;
		
		try {
			 serverSoket = new ServerSocket(5555);
			 
		} catch (IOException e) {
			System.err.println("다음의 포트를 번호에 연결할수 없습니다.: 5555");
			System.exit(-1);
		}
		
	
		
		try {
			clientSoket = serverSoket.accept();
		} catch (IOException e) {
			System.err.println("accept() fail");
			System.exit(-1);
			
		}
		out = new PrintWriter(clientSoket.getOutputStream(), true);
		in = new BufferedReader(new InputStreamReader(clientSoket.getInputStream()));
		
		String inputLIne;
		String outputLIne;
		
		QuizeProtocol qp = new QuizeProtocol();
		
		outputLIne = qp.process(null);
		out.println(outputLIne); //클라이언트에게 시작됨을 전송 " 퀴즈를 시작합니다. (y/n)"
		
		while((inputLIne = in.readLine()) != null){
			outputLIne = qp.process(inputLIne);
			out.println(outputLIne);
			if(outputLIne.equalsIgnoreCase("quit")){
				break;
			}
			in.close();
			out.close();
			clientSoket.close();
			serverSoket.close();
			
		}
	}
}
